<template>
  <div class="swiper-container">
    <Swiper
      class="my-swiper"
      :slides-per-view="1"
      :space-between="30"
      :navigation="navigation"
      :modules="[Navigation]"
      :loop="true"
      @swiper="onSwiper"
      @slide-change="onSlideChange"
    >
      <SwiperSlide class="my-swiper-slide" v-for="i in 5" :key="i">
        <div class="slide-content">
          <h3>轮播图 {{ i }}</h3>
          <p>这是第 {{ i }} 个轮播项的内容</p>
        </div>
      </SwiperSlide>
    </Swiper>

    <!-- 自定义导航按钮 -->
    <div class="custom-navigation">
      <button class="custom-prev" @click="slidePrev">
        <i class="icon-left">‹</i>
      </button>
      <div class="slide-info">
        <span class="current-slide">{{ currentSlide }}</span>
        <span class="divider">/</span>
        <span class="total-slides">{{ totalSlides }}</span>
      </div>
      <button class="custom-next" @click="slideNext">
        <i class="icon-right">›</i>
      </button>
    </div>
  </div>
</template>
<script setup lang="ts">
import { Swiper, SwiperSlide } from 'swiper/vue'
import 'swiper/css/navigation';
import 'swiper/css'
import { Navigation } from 'swiper/modules';
import { ref } from 'vue'

// 自定义导航配置
const navigation = {
  nextEl: '.custom-next',
  prevEl: '.custom-prev',
  disabledClass: 'swiper-button-disabled',
}
const emit = defineEmits(['swiper'])

// 响应式变量
const swiperInstance = ref<any>(null)
const currentSlide = ref(1)
const totalSlides = ref(5)

// Swiper 实例回调
const onSwiper = (swiper: any) => {
  swiperInstance.value = swiper
  console.log(swiper);
  emit('swiper', swiper)
}

// 滑动变化回调
const onSlideChange = (swiper: any) => {
  currentSlide.value = swiper.realIndex + 1
}

// 自定义导航方法
const slidePrev = () => {
  swiperInstance.value?.slidePrev()
}

const slideNext = () => {
  swiperInstance.value?.slideNext()
}
</script>
<style scoped lang="scss">
.swiper-container {
  width: 100%;
  height: 100%;
  position: relative;

  .my-swiper {
    width: 100%;
    height: 100%;
  }
}

.my-swiper-slide {
  // 此处是因为unocss和swiper样式有冲突，所以需要添加此样式
  width: 100% !important;
  text-align: center;
  font-size: 18px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;

  .slide-content {
    h3 {
      margin: 0 0 10px 0;
      font-size: 24px;
    }
    p {
      margin: 0;
      font-size: 16px;
      opacity: 0.9;
    }
  }
}

// 自定义导航样式
.custom-navigation {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 15px;
  background: rgba(0, 0, 0, 0.7);
  padding: 10px 20px;
  border-radius: 25px;
  z-index: 10;

  .custom-prev,
  .custom-next {
    width: 40px;
    height: 40px;
    border: none;
    background: rgba(255, 255, 255, 0.2);
    color: white;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    transition: all 0.3s ease;

    &:hover {
      background: rgba(255, 255, 255, 0.3);
      transform: scale(1.1);
    }

    &:active {
      transform: scale(0.95);
    }

    // 禁用状态
    &.swiper-button-disabled {
      opacity: 0.5;
      cursor: not-allowed;

      &:hover {
        background: rgba(255, 255, 255, 0.2);
        transform: none;
      }
    }
  }

  .slide-info {
    color: white;
    font-size: 14px;
    font-weight: 500;

    .current-slide {
      color: #67c23a;
    }

    .divider {
      margin: 0 5px;
      opacity: 0.7;
    }

    .total-slides {
      opacity: 0.7;
    }
  }
}

// 隐藏默认导航按钮
:deep(.swiper-button-prev),
:deep(.swiper-button-next) {
  display: none;
}
</style>
